package com.example.springboot.mybatis.hikaricp.util;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class LogMaskingUtils {

    private static final Pattern PHONE_PATTERN = Pattern.compile("(\\d{3})\\d{4}(\\d{4})");
    private static final Pattern ID_CARD_PATTERN = Pattern.compile("(\\d{6})\\d{8}(\\w{4})");
    private static final Pattern BANK_CARD_PATTERN = Pattern.compile("(\\d{4})\\d{8,12}(\\d{4})");
    private static final Pattern EMAIL_PATTERN = Pattern.compile("(\\w{1})\\w*@(\\w+\\.\\w+)");

    public static String maskPhone(String phone) {
        Matcher matcher = PHONE_PATTERN.matcher(phone);
        return matcher.replaceAll("$1****$2");
    }

    public static String maskIdCard(String idCard) {
        Matcher matcher = ID_CARD_PATTERN.matcher(idCard);
        return matcher.replaceAll("$1********$2");
    }

    public static String maskBankCard(String bankCard) {
        Matcher matcher = BANK_CARD_PATTERN.matcher(bankCard);
        return matcher.replaceAll("$1******$2");
    }

    public static String maskEmail(String email) {
        Matcher matcher = EMAIL_PATTERN.matcher(email);
        return matcher.replaceAll("$1*****@$2");
    }
}
